/**
 * PHOSIDE: PHosphorylation Site IDentification Engine.
 * Copyright 2009 Digital Biology Lab, University of Missouri.
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 3 of the License, or (at your option) any later
 * version. <p/> This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the License for more
 * details.
 */

package phoside.io.fasta;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.Writer;

import java.util.List;
import java.util.Set;

import phoside.Proteins;

import phoside.io.ProteinsWriter;

/**
 *
 * @author gjj
 */
public abstract class AbstractFastaProteinsWriter implements ProteinsWriter {
    protected Writer writer;

    public AbstractFastaProteinsWriter(final Writer writer) {
        this.writer = writer;
    }
    
    /**
     *
     * @param data
     */
    public void write(final Proteins data) throws IOException {
//        if (data==null || fastaDir==null) {
//            throw new NullPointerException();
//        }
//
//        Writer fout = new FileWriter(fastaDir);
        BufferedWriter out = new BufferedWriter(writer);

        Set<String> accessions = data.getProteinsAccessions();

        for (String acc : accessions) {
            out.write(formatHeader(data,acc));

            out.newLine();

            List<String> seqs = formatSequence(data,acc);
            if (seqs!=null) {
                for (String seq : seqs) {
                    out.write(seq);
                    out.newLine();
                }
            }
            out.newLine(); // add a empty line between entries;
        }

        out.close();
        writer.close();
    }

    protected abstract String formatHeader(final Proteins data, final String acc);

    protected abstract List<String> formatSequence(final Proteins data, final String acc);
}
